package huixue.cn.com.rxapp.api;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
import javax.xml.transform.Transformer;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import huixue.cn.com.library.RetrofitHttpClient;
import huixue.cn.com.library.entity.BaseHttpResult;
import huixue.cn.com.library.entity.BaseResponse;
import huixue.cn.com.library.http.UnSafeHostnameVerifier;
import huixue.cn.com.library.interceptor.LoggerIntercepter;
import huixue.cn.com.library.util.MD5;
import huixue.cn.com.rxapp.app.RxAppApplication;
import huixue.cn.com.rxapp.entity.DeviceInfo;
import huixue.cn.com.rxapp.entity.Gank;
import huixue.cn.com.rxapp.entity.HttpResult;
import huixue.cn.com.rxapp.interceptor.CommonInterceptor;
import huixue.cn.com.rxapp.kchart.bean.KLineData;
import huixue.cn.com.rxapp.login.entity.User;
import huixue.cn.com.rxapp.util.SignUtil;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Cache;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by gongdongyang on 2018/7/17.
 */

public class RetrofitService {
    //private static final String API_HOST = "http://gank.io/api/";
    //private static final String API_HOST = "https://apptest.ccxcredit.com/appTest/v16/";
    private static final String API_HOST = "https://btmxapp.co/";
    private static GankApi gankApi;
    private static ZCXApi zcxApi;
    private static BitApi httpApi;
    public static void init(){
        // 指定缓存路径,缓存大小100Mb
        Cache cache = new Cache(new File(RxAppApplication.getApplictionContext().getCacheDir(), "HttpCache"),
                1024 * 1024 * 100);
        RetrofitHttpClient.init(RxAppApplication.getApplictionContext());
        RetrofitHttpClient.getInstance()
                .setConnectTimeout(60 * 1000)
                .setRetryCount(3)//默认网络不好自动重试3次
                .setRetryDelay(500)//每次延时500ms重试
                .setRetryIncreaseDelay(500)//每次延时叠加500ms
                .setCertificates()//信任所有证书
                .setHostnameVerifier(new UnSafeHostnameVerifier(API_HOST))//全局访问规则
                .setBaseUrl(API_HOST)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                //.addInterceptor(new CommonInterceptor())
                .addInterceptor(new LoggerIntercepter())
                .setOkclient(RetrofitHttpClient.getOkHttpClient());
        //gankApi = RetrofitHttpClient.getRetrofit().create(GankApi.class);
        //zcxApi = RetrofitHttpClient.getRetrofit().create(ZCXApi.class);
        /*OkHttpClient mClient = new OkHttpClient.Builder()
                .addInterceptor(new LoggerIntercepter())//添加log拦截器
                .cache(cache)
                .connectTimeout(20, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .build();*/

        /*Retrofit mRetrofit = new Retrofit.Builder()
                .baseUrl(API_HOST)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//支持RxJava
                .client(mClient)
                .build();*/


        httpApi = RetrofitHttpClient.getRetrofit().create(BitApi.class);;
    }


    public static Observable<HttpResult<List<Gank>>> getNewsList(String type,int page){
        return gankApi.getGankData(type,page).subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread());
    }


    public static Observable<BaseHttpResult<String>> login(String loginName, String password){
        /*return zcxApi.login(loginName,password).subscribeOn(Schedulers.io())

        Map<String,String> map = new HashMap<String,String>();
        map.put("account",loginName);
        map.put("password",password);
        map.put("unionid","unionid");
        return zcxApi.login(SignUtil.makeSignMap(map)).subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread());*/
        return applySchedulers(zcxApi.login(loginName,password));
    }

    public static Observable<BaseResponse<List<KLineData>>> loadKLineDataV(String symbol, String interval, String from, String to) {
        return httpApi.loadKlineDataV1(symbol, interval, from, to);
    }


    public static Observable<HttpResult<List<Gank>>> loginExt(String type,int page){
        return gankApi.getGankData(type,page).subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread());
    }

    static <T> Observable<T>  applySchedulers(Observable<T> observable) {
        return observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread());
    }

    /*<T> Transformer<T, T> applySchedulers() {
        return new Transformer<T, T>() {
            @Override
            public Observable<T> call(Observable<T> observable) {
                return observable.subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }*/

}
